package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.MenuMapper;
import com.itheima.health.dao.RoleMapper;
import com.itheima.health.dao.UserMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@DubboService
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuDao menuDao;

    @Override
    public HashSet<Map<String, Object>> getMenuListByUsername(String username) {
        User user = userMapper.findByUserName(username);
        Set<Role> roles = roleMapper.findByUserId(user.getId());
        HashSet<Map<String, Object>> resultSet = new HashSet<>();


        for (Role role : roles) {
            List<Map<String, Object>> firstMenu = menuMapper.findFristByRoleId(role.getId());
            List<Map<String, Object>> secondMenu = menuMapper.findSecondByRoleId(role.getId());
            for (Map<String, Object> menu : firstMenu) {
                HashSet<Object> set = new HashSet<>();
                menu.put("children", set);
                for (Map<String, Object> stringObjectMap : secondMenu) {
                    if (stringObjectMap.get("parentMenuId") == menu.get("id")) {
                        Set set1 = (Set) menu.get("children");
                        set1.add(stringObjectMap);
                        menu.put("children", set1);
                    }
                }
            }
            resultSet.addAll(firstMenu);
        }
        return resultSet;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page pages = menuDao.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pages.getTotal());
        pageResult.setRows(pages.getResult());
        return pageResult;
    }

    @Override
    public int add(Menu menu) {
        int i = menuDao.insertSelective(menu);
        return i;
    }

    @Override
    public Menu findById(Integer id) {
        Menu byId = menuDao.findById(id);
        return byId;
    }

    @Override
    public int edit(Menu menu) {
        menuDao.deleteById(menu.getId());
        int i = menuDao.insertSelective(menu);
        return i;
    }

    @Override
    public int delete(Integer id) {
        int i = menuDao.deleteById(id);
        return i;
    }


}